package com.cssl.service.impl;


import com.cssl.domain.DtsCollect;

import com.cssl.domain.DtsFootprint;
import com.cssl.domain.dto.DtsFootprintDto;

import com.cssl.service.DtsFootprintService;
import com.cssl.mapper.DtsFootprintMapper;
import com.cssl.util.RedisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄鑫
 * @description 针对表【dts_footprint(用户浏览足迹表)】的数据库操作Service实现
 * @createDate 2024-08-03 09:20:50
 */
@Service
public class DtsFootprintServiceImpl
        implements DtsFootprintService {
    @Autowired
    private DtsFootprintMapper dtsFootprintMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map getFootprintList(DtsFootprintDto dtsFootprintDto) {
        // Redis Key 存储，包含页码和条件
        StringBuilder redisKeyBuilder = new StringBuilder("FootprintList");
        if (dtsFootprintDto.getUserId() != null && dtsFootprintDto.getUserId() != 0) {
            redisKeyBuilder.append(":UserId:").append(dtsFootprintDto.getUserId());
        }
        // 检查手机号
        if (dtsFootprintDto.getValueId() != null && dtsFootprintDto.getValueId() != 0) {
            redisKeyBuilder.append(":ValueId:").append(dtsFootprintDto.getValueId());
        }
        redisKeyBuilder.append(":page:").append(dtsFootprintDto.getPageNum());
        String redisKey = redisKeyBuilder.toString();
        List<DtsFootprint> footprintList = null;
        // 检查 Redis 中是否存在当前页的数据
        if (redisUtil.hasKey(redisKey)) {
            System.out.println("从 Redis 中获取数据");
            footprintList = (List<DtsFootprint>) redisUtil.get(redisKey);
            Map<String, Object> map1 = new HashMap<>();
            // 获取存储的总数

            map1.put("list", footprintList);
            map1.put("total", redisUtil.get("FootprintList:total")); // 使用从 Redis 获取的总数
            map1.put("pageNum", dtsFootprintDto.getPageNum());
            map1.put("pageSize", dtsFootprintDto.getPageSize());
            return map1;
        } else {
            // 开始分页
            Page page = PageHelper.startPage(dtsFootprintDto.getPageNum(), dtsFootprintDto.getPageSize());
            System.out.println("从数据库中查询数据");
            footprintList = dtsFootprintMapper.getFootprintList(dtsFootprintDto);
            Map<String, Object> map = new HashMap<>();
            // 计算总数
//            long total = page.getTotal();

            // 将查询结果放入 Redis，设置过期时间，例如：1小时
            redisUtil.set(redisKey, footprintList, 3600);
            redisUtil.set("FootprintList:total", dtsFootprintMapper.getDtsFootprintCount(), 3600);

            map.put("list", page.getResult());
            map.put("total", page.getTotal()); // 使用从数据库计算的总数
            map.put("totalPage", page.getPages());
            map.put("pageNum", page.getPageNum());
            map.put("pageSize", page.getPageSize());
            return map;
        }
    }

    @Override
    public void addFootPrint(Integer userId, int parseInt) {
        //判断是否含有相同的浏览足迹
        Integer flag=dtsFootprintMapper.hasCommentFoot(userId,parseInt);
       if (flag<=0){
           dtsFootprintMapper.addFootPrint(userId,parseInt);
       }else{
           dtsFootprintMapper.updateFootTime(userId,parseInt);
       }
    }
}




